package study.apm.arthas.controller;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.apm.arthas.service.CmdService;
import study.eggs.result.Result;
import study.eggs.result.Results;

@Slf4j
@RestController
@RequestMapping("/cmd")
public class CmdController {

    @Autowired
    private CmdService cmdService;

    /**
     * trace
     * https://www.163.com/dy/article/IL7LNANT0511CUMI.html
     * https://blog.csdn.net/cmw1085215666/article/details/129446061
     */
    @RequestMapping("/trace")
    public Result trace(long millis) {
        StrUtil.isEmpty("adsfasdf");
        StrUtil.isEmpty("adsfasdf");
        cmdService.sleep(millis);
        StrUtil.isEmpty("adsfasdf");
        StrUtil.isEmpty("adsfasdf");
        return Results.ok();
    }

    /**
     * watch
     */
    @RequestMapping("/watch")
    public Result watch(@RequestBody Request request) {
        log.info(">>>>>> 请求参数: {}", request);
        //
        Boolean throwEx = request.getThrowEx();
        if (throwEx) {
            throw new RuntimeException("我是一个自定义异常");
        }
        //
        Response resp = new Response();
        resp.setName("fsafdasd");
        return Results.ok(resp);
    }

    @Data
    public static class Request {
        private String name;
        private int age;
        private Boolean throwEx;
    }

    @Data
    public static class Response {
        private String name;
        private int age;
    }
}
